<template>
  <el-form ref="basicFormRef" class="w-100 ma-a pt-4" :model="basicForm" :rules="rules" size="small" label-width="80px">
    <el-form-item label="机型" prop="modelType">
      <el-select v-model="basicForm.modelType" clearable>
        <el-option v-for="(item, index) in pageAircraftTypes" :key="'modelType' + index" :label="item.label" :value="item.label" />
      </el-select>
    </el-form-item>
    <el-form-item label="载荷类型" prop="loadName">
      <el-select v-model="basicForm.loadName" clearable @change="handleLoadNameChange">
        <el-option v-for="(item, index) in pageLoadNames" :key="'type' + index" :label="item.label" :value="item.label" />
      </el-select>
    </el-form-item>
    <el-form-item label="载荷分类" prop="loadType">
      <el-select v-model="basicForm.loadType" clearable @change="getVersion">
        <el-option v-for="(item, index) in pageLoadTypes" :key="'name' + index" :label="item.label" :value="item.label" />
      </el-select>
    </el-form-item>
    <el-form-item label="阶段" prop="stage">
      <el-select v-model="basicForm.stage" clearable @change="getVersion">
        <el-option v-for="(item, index) in pageStages" :key="'stage' + index" :label="item.label" :value="item.label" />
      </el-select>
    </el-form-item>
    <el-form-item label="版本号" prop="version">
      <el-input v-model="basicForm.version" placeholder="请输入版本号" clearable />
    </el-form-item>
    <el-form-item label="轮次" prop="round">
      <el-input v-model="basicForm.round" placeholder="请输入轮次" clearable />
    </el-form-item>
  </el-form>
</template>

<script setup name="VersionBasicForm" lang="ts">
import { getLatestVersion, addVersion, getVersionDetail } from '@/api/loadProcessingAlgorithm/loadVersionManagement/index';
import { loadNames, stages, aircraftTypes } from '@/api/loadProcessingAlgorithm/index';
import { backProcess } from '@/api/workflow/task/index';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { dataId, type } = defineProps({
  dataId: String,
  type: String
});
const basicFormRef = ref<ElFormInstance>();
const pageLoadNames = ref(loadNames);
const pageLoadTypes = ref([]);
const pageStages = ref(stages);
const pageAircraftTypes = ref(aircraftTypes);
const basicForm = reactive({
  loadType: '',
  loadName: '',
  stage: '',
  version: '',
  round: '',
  modelType: ''
});
const rules = reactive({
  loadType: [{ required: true, message: '载荷分类不能为空', trigger: 'change' }],
  loadName: [{ required: true, message: '载荷类型不能为空', trigger: 'change' }],
  stage: [{ required: true, message: '阶段不能为空', trigger: 'change' }],
  version: [{ required: true, message: '版本不能为空', trigger: 'blur' }],
  modelType: [{ required: true, message: '机型不能为空', trigger: 'change' }],
  round: [{ required: true, message: '轮次不能为空', trigger: 'blur' }]
});
const handleLoadNameChange = () => {
  if (basicForm.loadName) {
    const obj = pageLoadNames.value.find((item) => item.label === basicForm.loadName);
    pageLoadTypes.value = obj ? obj.children : [];
  } else {
    pageLoadNames.value.forEach((item) => {
      pageLoadTypes.value = [...pageLoadTypes.value, ...item.children];
    });
  }
};
const getVersion = async () => {
  if (basicForm.loadName && basicForm.loadType && basicForm.stage && basicForm.modelType) {
    const obj = {
      loadType: basicForm.loadType,
      loadName: basicForm.loadName,
      stage: basicForm.stage,
      modelType: basicForm.modelType
    };
    const res = await getLatestVersion(obj);
    basicForm.version = res.data;
  }
};
const submitData = async () => {
  if (!basicFormRef.value) return null;
  let data = null;
  await basicFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      const res = await addVersion(basicForm);
      data = res.data;
    }
  });
  return data;
};
defineExpose({
  submitData
});
onMounted(async () => {
  if (dataId) {
    const { data } = await getVersionDetail(dataId);
    for (const key in basicForm) {
      basicForm[key] = data[key];
    }
  }

  handleLoadNameChange();
});
</script>
<style lang="scss" scoped></style>
